#include "mytextedit.h"

// 遍历文档结构（以获取文字和图片）
void CustomTextEdit::getTextAndImage()
{
    QTextDocument *doc = document();
    for (QTextBlock block = doc->begin(); block != doc->end(); block = block.next())
    {
        for (QTextBlock::iterator it = block.begin(); !it.atEnd(); ++it)
        {
            QTextFragment fragment = it.fragment();
            if (fragment.isValid() && fragment.charFormat().isImageFormat())
            {
                QTextImageFormat image_format = fragment.charFormat().toImageFormat();
                if(doc->resource(QTextDocument::ImageResource, image_format.name()).canConvert<QPixmap>())
                {
                    QString type = doc->resource(QTextDocument::ImageResource, image_format.name()).typeName();
                    QVariant var = doc->resource(QTextDocument::ImageResource, image_format.name());
                    QString imgName = image_format.name();
                    //取出图片
                    QImage image = QImage(var.value<QImage>());
                    QPixmap map = QPixmap().fromImage(image);
                    map.save("1.png");
                }
            }
            else
            {
                QString text = fragment.text();
                // 处理文字片段
                qDebug()<<text<<endl;
            }
        }
    }
}
